package com.ly.hotel.spainterface.jxd.activity;

import android.app.FragmentManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IdRes;
import androidx.core.content.ContextCompat;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.jg.view.CustomToast;
import com.ly.hotel.spainterface.jxd.adapter.MassageAdapter;
import com.ly.hotel.spainterface.jxd.adapter.TherapyListAdapter;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.jxd.bean.Massage;
import com.ly.hotel.spainterface.jxd.bean.PluginBean;
import com.ly.hotel.spainterface.jxd.bean.SortByArtNo;
import com.ly.hotel.spainterface.jxd.bean.Therapy;
import com.ly.hotel.spainterface.jxd.dialog.JxdJSDetailDialog;
import com.ly.hotel.spainterface.jxd.dialog.JxdRecommendDialog;
import com.ly.hotel.spainterface.jxd.dialog.JxdSearchTherapyDialog;
import com.ly.hotel.spainterface.jxd.event.JxdShowMoreTherapyEvent;
import com.ly.hotel.spainterface.jxd.dialog.OnClickTherapyListener;
import com.ly.hotel.spainterface.jxd.utils.AvoidDoubleClickedUtils;
import com.ly.hotel.spainterface.jxd.utils.MachineUtils;
import com.ly.hotel.spainterface.jxd.utils.UdpUtils;
import com.ly.hotel.spainterface.jxd.utils.XmlParseUtils;
import com.ly.hotel.util.ProcessWaitingUtil;
import com.ly.hotel.view.TitleLayout;
import com.ly.hotel.view.common.BaseActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by ThinkPad on 2018/3/20.
 */

public class JxdJSActivity extends BaseActivity implements OnClickTherapyListener{

    private static final String TAG = "JxdJSActivity";
    private final int GET_NO_HALL_ITEM = 0,GET_HALL_ITEM = 1,GET_THERAPY_DATA = 2,GET_THERAPY_DATA_BY_ARTNO = 8;
    private MyApplication app;
    private PluginBean pluginBean = null;
    private String cashierIP = "";
    private String cashierPORT = "";
    private ExpandableListView massageItemListView;
    private List<String> massageCategoryGroup = null;
    private List<List<Massage>> massageNamesChild= new ArrayList<>();
    private Massage massage;
    private RelativeLayout downMoreTips;
    private RelativeLayout itemDownMoreTips;
    private FragmentManager fm;
    private RelativeLayout noDataTips;
    private MachineBean machineBean;// 保存电视信息bean
    private ImageButton recommendTherapy;
    private ImageButton searchTherapy;
    private List<Massage> hallMassage = new ArrayList<>();
    private List<Massage> noHallMassage = new ArrayList<>();
    private LinearLayout recommendLinearLayout;
    private boolean isShowHallItem = false;// 是否显示大堂项目
    private boolean isShowNoHallItem = false;// 是否显示全身项目
    private LinearLayout searchLinearLayout;
    private RadioGroup statusGroup;
    private RadioButton freeSelect;
    private RadioButton workingSelect;
    private RadioButton allSelect;
    private RadioGroup sexGroup;
    private RadioButton sexAll;
    private RadioButton sexFemale;
    private RadioButton sexMale;
    private String status = "空闲";
    private String sex = "全部";
    private List<Therapy> therapies;
    private GridView statusGridView;
    private RelativeLayout noThisStatusTips;
    private String massageName;
    private TherapyListAdapter statusAdapter;
    private RelativeLayout statusListDownMoreTips;
    private LinearLayout therapyListLinearlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jxd_js_activity);
        app = MyApplication.getInstance();
        fm = getFragmentManager();
        ProcessWaitingUtil.showProcessDialog(this,getResources().getString(R.string.loading_tips),10000);
        getMachineInfo();
        getPluginConfig();
        initView();
    }

    // 获得电视相关信息
    public void getMachineInfo(){
        machineBean = MachineUtils.getHostAndTvInfo();
        /*if(machineBean != null){
            Log.i(TAG,"serverIP=" +  machineBean.getServerIP());
            Log.i(TAG,"serverPORT=" +  machineBean.getServerPORT());
            Log.i(TAG,"mac=" +  machineBean.getMac());
            Log.i(TAG,"categoryName=" +  machineBean.getCategoryName());
            Log.i(TAG,"tvName=" +  machineBean.getTvName());
        }*/
    }

    // 获取插件配置
    public void getPluginConfig(){
        //Log.i(TAG,"插件配置：" + MyApplication.pluginCfg);
        Log.i(TAG,"插件版本：" + MyApplication.interfaceVersion);
        pluginBean = XmlParseUtils.getInstance().parsePluginConfig(this,MyApplication.pluginCfg,MyApplication.interfaceVersion);
        if(pluginBean != null){
            cashierIP = pluginBean.getIp();
            cashierPORT = pluginBean.getPort();
            if(!TextUtils.isEmpty(cashierIP) && !TextUtils.isEmpty(cashierPORT)){
                massageCategoryGroup = new ArrayList<>();
                if(!TextUtils.isEmpty(pluginBean.getItemclass1())){
                    try{
                        String[] itemClass1 = pluginBean.getItemclass1().split(",");
                        if(itemClass1[1].equals("true")){
                            massageCategoryGroup.add(itemClass1[0]);
                            isShowHallItem = true;
                        }
                    }catch (Exception e){
                        //massageCategoryGroup.add("大堂项目");
                        //Log.i(TAG,"itemClass1配置出错！");
                    }
                }else{
                    massageCategoryGroup.add("大堂项目");
                }

                if(!TextUtils.isEmpty(pluginBean.getItemclass2())){
                    try{
                        String[] itemClass2 = pluginBean.getItemclass2().split(",");
                        if(itemClass2[1].equals("true")){
                            massageCategoryGroup.add(itemClass2[0]);
                            isShowNoHallItem = true;
                        }
                    }catch (Exception e){
                        //massageCategoryGroup.add("全身项目");
                        //Log.i(TAG,"itemClass2配置出错！");
                    }
                }else{
                    massageCategoryGroup.add("全身项目");
                }

                // 当itemClass1和itemClass2配置中都为false的是否，默认显示全部
                if(massageCategoryGroup.size() == 0){
                    massageCategoryGroup.add("大堂项目");
                    massageCategoryGroup.add("全身项目");
                }
                getHallItem();
            }else{
                Toast.makeText(this, "ip或port配置有误!", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this, "请检查后台配置是否有误！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ProcessWaitingUtil.closeProcessDialog();
    }

    public void onEventMainThread(JxdShowMoreTherapyEvent event) {
        //Log.d(TAG,"收到事件返回" + event.isHideMoreTips());
        if(event.isHideMoreTips()){
            downMoreTips.setVisibility(View.VISIBLE);
        }else{
            downMoreTips.setVisibility(View.GONE);
        }
    }

    // 初始化View
    private void initView() {
        // 初始化标题栏
        TitleLayout titleLayout = findViewById(R.id.title_layout);
        TextView titleName = titleLayout.findViewById(R.id.activity_name);
        titleName.setTextColor(ContextCompat.getColor(this,R.color.mainWhite));
        titleName.setText(getResources().getString(R.string.naviJishiName));

        // 初始化左侧大堂和全身项目View
        massageItemListView = findViewById(R.id.spa_item_expandable_listview);
        massageItemListView.setVerticalScrollBarEnabled(false);// 隐藏滚动条
        itemDownMoreTips = findViewById(R.id.item_down_more_tips);
        noDataTips = findViewById(R.id.no_data_tips);

        // 初始化状态和性别RadioButton
        statusGroup = findViewById(R.id.status_select);
        freeSelect = findViewById(R.id.free_select);
        workingSelect = findViewById(R.id.working_select);
        allSelect = findViewById(R.id.all_select);
        statusGroup.setOnCheckedChangeListener(new StatusRadioButtonListener());

        sexGroup = findViewById(R.id.sex_select);
        sexAll = findViewById(R.id.sex_all);
        sexFemale = findViewById(R.id.sex_female);
        sexMale = findViewById(R.id.sex_male);
        sexGroup.setOnCheckedChangeListener(new SexRadioButtonListener());

        // 初始化技师状态GridView
        statusGridView = findViewById(R.id.status_gridview);
        noThisStatusTips = findViewById(R.id.no_this_status_tips);
        statusListDownMoreTips = findViewById(R.id.down_more_tips);
        therapyListLinearlayout = findViewById(R.id.therapy_list_linearlayout);

        // 初始化推荐技师和查询技师
        recommendTherapy = findViewById(R.id.recommend_therapy);
        searchTherapy = findViewById(R.id.search_therapy);
        recommendLinearLayout = findViewById(R.id.recommend_linearlayout);
        searchLinearLayout = findViewById(R.id.search_linearlayout);
        recommendTherapy();
        searchTherapy();
        //initStatusFragment();
    }

    // 推荐技师
    public void recommendTherapy(){
        recommendTherapy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){
                    return;
                }
                JxdRecommendDialog recommendDialog = new JxdRecommendDialog();
                recommendDialog.setCancelable(false);
                recommendDialog.setMachineBean(machineBean);
                recommendDialog.setPluginBean(pluginBean);
                recommendDialog.setHallMassage(hallMassage);
                recommendDialog.show(getFragmentManager(),"JxdRecommendDialog");
            }
        });
    }

    // 查询技师
    public void searchTherapy(){
        searchTherapy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){
                    return;
                }
                JxdSearchTherapyDialog searchTherapyDialog = new JxdSearchTherapyDialog();
                searchTherapyDialog.setPluginBean(pluginBean);
                searchTherapyDialog.setMachineBean(machineBean);
                searchTherapyDialog.setHallMassage(hallMassage);
                searchTherapyDialog.setNoHallMassage(noHallMassage);
                searchTherapyDialog.setCancelable(false);
                searchTherapyDialog.show(getFragmentManager(),"JxdSearchTherapyDialog");
            }
        });
    }

    class StatusRadioButtonListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId) {
                case R.id.free_select:
                    status = "空闲";
                    ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this, getResources().getString(R.string.loading_tips), 10000);
                    getTherapyData();
                    break;

                case R.id.working_select:
                    status = "在做钟";
                    ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this, getResources().getString(R.string.loading_tips), 10000);
                    getTherapyData();
                    break;

                case R.id.all_select:
                    status = "全部";
                    ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this, getResources().getString(R.string.loading_tips), 10000);
                    getTherapyData();
                    break;

                default:
                    break;
            }
        }
    }

    class SexRadioButtonListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){
                case R.id.sex_all:
                    sex = "全部";
                    ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData();
                    break;

                case R.id.sex_female:
                    sex = "女";
                    ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData();
                    break;

                case R.id.sex_male:
                    sex = "男";
                    ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this,getResources().getString(R.string.loading_tips),10000);
                    getTherapyData();
                    break;

                default:
                    break;
            }
        }
    }

    // 请求大堂项目数据
    public void getHallItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>1</IsHall><OnlyMassageName>0</OnlyMassageName>" +
                "<SpecSetting>1</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(cashierIP,cashierPORT,req,handler,GET_HALL_ITEM);
    }

    // 请求全身项目数据
    public void getNoHallItem(){
        String req = "<gs ver=\"1.0\" function=\"桑拿做钟类型\" mode = \"request\"><MassageTypeNo>" +
                "</MassageTypeNo><MassageName></MassageName><IsHall>0</IsHall><OnlyMassageName>0</OnlyMassageName>" +
                "<SpecSetting>1</SpecSetting></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(cashierIP,cashierPORT,req,handler,GET_NO_HALL_ITEM);
    }

    public void getTherapyData(){
        Log.i(TAG,"status=" + status);
        Log.i(TAG,"massageName=" + massageName);
        String req = "<gs ver=\"1.0\" function=\"技师资料查询\" mode=\"request\"><ArtNo></ArtNo>" +
                    "<Status>" + status + "</Status><Onduty>全部</Onduty><ArtModeNo>" + massageName + "</ArtModeNo>" +
                    "<Columns>artNo,downminute,status,onduty,cname,sex</Columns><ArtTypeNo></ArtTypeNo></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_THERAPY_DATA);
    }

    // 请求技师数据
    public void getTherapyDataByArtNo(String artNo){
        ProcessWaitingUtil.showProcessDialog(this,getResources().getString(R.string.loading_tips),10000);
        Log.i(TAG,"artNo=" + artNo);
        String req = "<gs ver=\"1.0\" function=\"技师资料查询\" mode = \"request\"><ArtNo>" + artNo + "</ArtNo><Status>" +
                "</Status><Onduty></Onduty><ArtModeNo></ArtModeNo><Columns></Columns><ArtTypeNo>" +
                "</ArtTypeNo></gs>";
        UdpUtils udpUtils = new UdpUtils();
        udpUtils.getUdpResult(pluginBean.getIp(),pluginBean.getPort(),req,handler,GET_THERAPY_DATA_BY_ARTNO);
    }

    // 设置做钟类型Adapter
    public void setMassageAdapter(){
        if(hallMassage.size() == 0 && noHallMassage.size() == 0){
            noDataTips.setVisibility(View.VISIBLE);
            // 设置状态和性别选择栏不可见
            therapyListLinearlayout.setVisibility(View.GONE);
        }else{
            if(massageCategoryGroup.size() == massageNamesChild.size()){
                // 设置状态和性别选择栏可见
                therapyListLinearlayout.setVisibility(View.VISIBLE);
                // 设置推荐技师和查询技师可见
                recommendLinearLayout.setVisibility(View.VISIBLE);
                searchLinearLayout.setVisibility(View.VISIBLE);

                // 根据配置设置推荐技师是否可见
                if(pluginBean != null){
                    if(!TextUtils.isEmpty(pluginBean.getRecommend())){
                        if(pluginBean.getRecommend().equals("false")){
                            recommendLinearLayout.setVisibility(View.GONE);
                        }
                    }
                }
                final MassageAdapter massageAdapter = new MassageAdapter(this,massageCategoryGroup,massageNamesChild);
                massageItemListView.setAdapter(massageAdapter);
                if(massageAdapter != null){// 默认打开所有组
                    for(int i = 0;i < massageCategoryGroup.size();i ++){
                        massageItemListView.expandGroup(i);
                    }
                }
                // 监听按摩项目的点击事件
                massageItemListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                        ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this,getResources().getString(R.string.loading_tips),10000);
                        massageAdapter.setDefaultGroupPostion(groupPosition);
                        massageAdapter.setDefaultChildPosition(childPosition);
                        //Log.d(TAG,"默认点击第一项：" + groupPosition + "-" + childPosition);
                        List<Massage> massages = massageNamesChild.get(groupPosition);
                        massage = massages.get(childPosition);
                        massageName = massage.getMassagename();
                        Log.i(TAG,"点击的做钟项目：" + massageName);
                        //Log.i(TAG,"是否是大堂项目：" + massage.getIshall());
                        ProcessWaitingUtil.showProcessDialog(JxdJSActivity.this,getResources().getString(R.string.loading_tips),10000);
                        getTherapyData();
                        return true;
                    }
                });
                // 认点击childView的第一项
                massageItemListView.performItemClick(null,1,0);// 第三个参数为零，第二个参数为从第0个groupView开始数起

                massageItemListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {

                    }

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                        //Log.i(TAG,"firstVisibleItem=" + firstVisibleItem + " visibleItemCount=" + visibleItemCount + " totalItemCount=" + totalItemCount);
                        if((firstVisibleItem == 0 && visibleItemCount == totalItemCount) || firstVisibleItem > 0 && firstVisibleItem + visibleItemCount == totalItemCount){
                            itemDownMoreTips.setVisibility(View.GONE);
                        }else{
                            itemDownMoreTips.setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Log.i(TAG,"接口返回：" + msg.obj.toString());
                    String resultString = msg.obj.toString();
                    if(msg.arg1 == GET_HALL_ITEM){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            Log.i(TAG,"没有查询到大堂做钟类型信息");
                        }else{
                            hallMassage = XmlParseUtils.getInstance().parseMassageItem(resultString);

                            // 根据配置隐藏不需要显示的做钟类型
                            if(!TextUtils.isEmpty(pluginBean.getServeDel())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serverDel = pluginBean.getServeDel().split(",");
                                    for(int i = 0;i < hallMassage.size();i ++){
                                        Massage massage = hallMassage.get(i);
                                        for(int j = 0;j < serverDel.length;j ++){
                                            if(massage.getMassagename().equals(serverDel[j])){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        hallMassage.removeAll(tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            // 根据配置对项目进行排序
                            if(!TextUtils.isEmpty(pluginBean.getServeSort())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serveSort = pluginBean.getServeSort().split(",");
                                    for(int i = 0;i < serveSort.length;i ++){
                                        for(int j = 0;j < hallMassage.size();j ++){
                                            Massage massage = hallMassage.get(j);
                                            if(serveSort[i].equals(massage.getMassagename())){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        hallMassage.removeAll(tempMassage);
                                        hallMassage.addAll(0,tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            // 将大堂项目加入到总的项目中
                            if(hallMassage.size() > 0 && isShowHallItem == true){
                                massageNamesChild.add(hallMassage);
                            }

                            // 如果都为itemClass1和itemClass2的配置都为false的时，则默认显示大堂和全身
                            if(isShowNoHallItem == false && isShowHallItem == false){
                                massageNamesChild.add(hallMassage);
                            }
                        }
                        getNoHallItem();
                    }else if(msg.arg1 == GET_NO_HALL_ITEM){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            Log.i(TAG,"没有查询到全身做钟类型信息");
                        }else{
                            noHallMassage = XmlParseUtils.getInstance().parseMassageItem(resultString);
                            // 根据配置隐藏不需要显示的做钟类型
                            if(!TextUtils.isEmpty(pluginBean.getServeDel())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serverDel = pluginBean.getServeDel().split(",");
                                    for(int i = 0;i < noHallMassage.size();i ++){
                                        Massage massage = noHallMassage.get(i);
                                        for(int j = 0;j < serverDel.length;j ++){
                                            if(massage.getMassagename().equals(serverDel[j])){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        noHallMassage.removeAll(tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            // 根据配置对项目进行排序
                            if(!TextUtils.isEmpty(pluginBean.getServeSort())){
                                try{
                                    List<Massage> tempMassage = new ArrayList<>();
                                    String[] serveSort = pluginBean.getServeSort().split(",");
                                    for(int i = 0;i < serveSort.length;i ++){
                                        for(int j = 0;j < noHallMassage.size();j ++){
                                            Massage massage = noHallMassage.get(j);
                                            if(serveSort[i].equals(massage.getMassagename())){
                                                tempMassage.add(massage);
                                            }
                                        }
                                    }
                                    if(tempMassage.size() > 0){
                                        noHallMassage.removeAll(tempMassage);
                                        noHallMassage.addAll(0,tempMassage);
                                    }
                                }catch (Exception e){

                                }
                            }

                            if(noHallMassage.size() > 0 && isShowNoHallItem == true){
                                massageNamesChild.add(noHallMassage);
                            }

                            // 如果都为itemClass1和itemClass2的配置都为false的时，则默认显示大堂和全身
                            if(isShowNoHallItem == false && isShowHallItem == false){
                                massageNamesChild.add(noHallMassage);
                            }
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                        setMassageAdapter();
                    }else if(msg.arg1 == GET_THERAPY_DATA){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            Log.i(TAG,"没有查询到技师资料!");
                            if(TextUtils.isEmpty(resultString)){
                                CustomToast customToast = CustomToast.makeText(JxdJSActivity.this,
                                        getResources().getString(R.string.connect_err),Toast.LENGTH_SHORT,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.show();
                                if(therapies != null){
                                    therapies.clear();
                                }
                                if(statusAdapter != null){
                                    statusAdapter.notifyDataSetChanged();
                                }
                                noDataTips.setVisibility(View.VISIBLE);
                            }else{
                                if(therapies != null){
                                    therapies.clear();
                                }
                                if(statusAdapter != null){
                                    statusAdapter.notifyDataSetChanged();
                                }
                                noThisStatusTips.setVisibility(View.VISIBLE);
                            }
                        }else{
                            therapies = XmlParseUtils.getInstance().parseTherapy(resultString);

                            // 筛选出不是空闲和当值的技师
                            if(status.equals("空闲")){
                                List<Therapy> tempTherapy = new ArrayList<>();
                                for(int i = 0; i < therapies.size(); i ++){
                                    Therapy therapy = therapies.get(i);
                                    if(therapy.getOnduty().equals("未当值") || therapy.getOnduty().equals("当值") && therapy.getStatus().equals("倒牌")){
                                        tempTherapy.add(therapy);
                                    }
                                }
                                therapies.removeAll(tempTherapy);
                            }

                            // 根据配置是否对技师进行排序
                            if(pluginBean != null){
                                if(pluginBean.isArtNumSort()){
                                    try {
                                        Collections.sort(therapies,new SortByArtNo());
                                    }catch (Exception e){

                                    }
                                }
                            }

                            if(sex.equals("女")){// 筛选出女技师
                                List<Therapy> tempTherapy = new ArrayList<>();
                                for(int i = 0; i < therapies.size(); i ++){
                                    Therapy therapy = therapies.get(i);
                                    if(!TextUtils.isEmpty(therapy.getSex())){
                                        if(therapy.getSex().equals("男")){
                                            tempTherapy.add(therapy);
                                        }
                                    }
                                }
                                therapies.removeAll(tempTherapy);
                            }else if(sex.equals("男")){// 筛选出男技师
                                List<Therapy> tempTherapy = new ArrayList<>();
                                for(int i = 0; i < therapies.size(); i ++){
                                    Therapy therapy = therapies.get(i);
                                    if(!TextUtils.isEmpty(therapy.getSex())){
                                        if(therapy.getSex().equals("女")){
                                            tempTherapy.add(therapy);
                                        }
                                    }
                                }
                                therapies.removeAll(tempTherapy);
                            }
                            if(therapies.size() == 0){
                                noThisStatusTips.setVisibility(View.VISIBLE);
                            }else{
                                noThisStatusTips.setVisibility(View.GONE);
                            }

                            statusAdapter = new TherapyListAdapter(JxdJSActivity.this,therapies,massage.getIshall(),pluginBean);
                            statusGridView.setAdapter(statusAdapter);
                            statusGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Therapy therapy = therapies.get(position);
                                    getTherapyDataByArtNo(therapy.getArtNo());
                                }
                            });

                            statusGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
                                @Override
                                public void onScrollStateChanged(AbsListView view, int scrollState) {

                                }

                                @Override
                                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                                    if((firstVisibleItem == 0 && visibleItemCount == totalItemCount) || firstVisibleItem > 0 && firstVisibleItem + visibleItemCount == totalItemCount){
                                        statusListDownMoreTips.setVisibility(View.GONE);
                                    }else{
                                        statusListDownMoreTips.setVisibility(View.VISIBLE);
                                    }
                                }
                            });
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                    }else if(msg.arg1 == GET_THERAPY_DATA_BY_ARTNO){
                        if(resultString.startsWith("Msg") || TextUtils.isEmpty(resultString)){
                            CustomToast customToast = CustomToast.makeText(JxdJSActivity.this,getResources().getString(R.string.connect_err),Toast.LENGTH_SHORT,false);
                            customToast.setGravity(Gravity.CENTER,0,0);
                            customToast.show();
                        }else{
                            List<Therapy> therapyList = XmlParseUtils.getInstance().parseTherapy(resultString);
                            if(therapyList.size() > 0){
                                setDialog(therapyList);
                            }else{
                                CustomToast customToast = CustomToast.makeText(JxdJSActivity.this,getResources().getString(R.string.connect_err),Toast.LENGTH_SHORT,false);
                                customToast.setGravity(Gravity.CENTER,0,0);
                                customToast.show();
                            }
                        }
                        ProcessWaitingUtil.closeProcessDialog();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public void setDialog(List<Therapy> therapyList){
        JxdJSDetailDialog detailDialog = new JxdJSDetailDialog();
        detailDialog.setPluginBean(pluginBean);
        detailDialog.setMachineBean(machineBean);
        detailDialog.setTherapies(therapyList);
        detailDialog.setIsHall(massage.getIshall());
        detailDialog.setCancelable(false);
        EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_JISHI + "\t" + MyApplication.interfaceVersion + "\t" + "选技师"));
        detailDialog.show(fm,"JxdJSDetailDialog");
    }

    @Override
    public void setTherapData(Therapy therapy,String isHall) {
        if(therapy != null){
            if(AvoidDoubleClickedUtils.isNotDoubleClicked()){
                return;
            }
            ProcessWaitingUtil.showProcessDialog(this,getResources().getString(R.string.loading_tips),10000);
            getTherapyDataByArtNo(therapy.getArtNo());
        }
    }
}
